<template>
  <div class='container'>
    <!-- 搜索框 -->
    <el-form :inline="true" :model="formInline">
      <el-form-item label="标签名称">
        <el-input v-model="formInline.tags" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="城市">
        <el-select @focus="openSelect" @change="changeSelect" v-model="formInline.province" placeholder="请选择">
            <el-option v-for=" (item,index) in formInline.Pivlist" :key="index" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="地区">
        <el-select @focus="openSelect2" @change="changeSelect2" v-model="formInline.City" placeholder="请选择">
            <el-option v-for="(item,index) in formInline.Clist" :key="index" :label="item"  :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="企业简称">
        <el-input v-model="formInline.shortName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.state" placeholder="请选择">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="btn">
        <el-button @click="reset">清除</el-button>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button class="add" type="success" @click="addUser">
          <i class="el-icon-edit"></i>
          新增用户</el-button>
      </el-form-item>
    </el-form>
    <div class="count">
      <i class="el-icon-info"></i>
      共 {{page.total}} 条记录
    </div>
  <!-- 列表 -->
  <el-table
      :data="Data"
      style="width: 100%">
      <el-table-column prop="id" label="序号" width="80">
      </el-table-column>
      <el-table-column prop="number" label="企业编号" width="100">
      </el-table-column>
      <el-table-column prop="shortName" label="企业简称" width="100">
      </el-table-column>
      <el-table-column prop="tags" label="标签" width="100">
      </el-table-column>
      <el-table-column prop="creatorID" label="创建者" width="80">
      </el-table-column>
      <el-table-column prop="addDate" label="创建日期" width="120">
      </el-table-column>
      <el-table-column prop="remarks" label="备注" width="150">
      </el-table-column>
      <el-table-column label="状态" width="100">
        <template v-slot={row}>
          {{ Newstate(row.state) }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot={row}>
          <el-button type="primary" style="opacity: .5;" icon="el-icon-edit" @click="edit(row)" circle></el-button>
          <el-tooltip class="item" effect="dark" content="启用" placement="top">
            <el-button type="success" style="opacity: .5;" icon="el-icon-check" v-if="row.state === 0" @click="isState(row)" circle></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="禁用" placement="top">
            <el-button type="warning" style="opacity: .5;" icon="el-icon-close" v-if="row.state === 1" @click="isState(row)" circle></el-button>
          </el-tooltip>
          <el-button type="danger" style="opacity: .5;" icon="el-icon-delete" @click="del(row)" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
     <el-pagination
     style="text-align:right; margin-top:20px"
      @next-click = next(1)
      @prev-click = prev(-1)
      :current-page="+page.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="prev, pager, next,sizes, jumper"
      :total="page.total">
    </el-pagination>
    <!-- 状态 -->
    <el-dialog
    class="del"
      title="提示"
      :visible.sync="Svisible"
        width="30%">
      <span v-if="row.state === 0">
        <i class="el-icon-warning"></i>
        已成功启用,是否继续?</span>
      <span v-if="row.state === 1">
        <i class="el-icon-warning"></i>
        已成功禁用,是否继续?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="wait">取 消</el-button>
        <el-button type="primary" @click="stateSubmit(row.state)">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 预览 -->
    <!-- 新增和修改 -->
    <Companys :visible.sync='visible' :row="row" :titleInfo="titleInfo" :formBase.sync="formBase" @newDataes="addSubmit(formBase)"/>
  </div>
</template>

<script>
import data, { provinces, citys } from '@/api/hmmm/citys'

import Companys from '../components/companys-add.vue'
import { list, remove, detail } from '@/api/hmmm/companys'
export default {
  components: {
    Companys
    // 还需要给组件传数据
  },
  data () {
    return {
      titleInfo: {
        text: '',
        pageTitle: ''
      },
      formBase: { },
      visible: false,
      Svisible: false,
      drawerType: '新增文章',
      videoSrc: '',
      videoState: false,
      row: {},
      page: {
        page: 1,
        pagesize: 10,
        total: 0,
        pages: 0 // 总页数
      },
      formInline: {
        tags: '',
        Pivlist: '',
        Clist: '',
        shortName: '',
        state: ''
      },
      Data: [],
      Pdata: {},
      tableData: {
        title: '', // 标题
        visits: '', // 阅读人数
        username: '', // 录入人
        createTime: '', // 录入时间
        state: 0// 状态
      }

    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList () {
      const { data } = await list(this.page)
      this.Data = data.items
      this.page.total = data.counts
      this.page.page = data.page
      this.page.pagesize = data.pagesize
      this.page.pages = data.pages
      // console.log(data)
    },
    async onSubmit () {
      // console.log(this.formInline)
      const res = await list({
        tags: this.formInline.tags,
        province: this.formInline.Pivlist,
        city: this.formInline.Clist,
        shortName: this.formInline.shortName,
        state: this.formInline.state,
        page: this.page.page,
        pagesize: this.page.pagesize
      })
      this.Data = res.data.items
    },
    reset () {
      this.formInline = {
        tags: '',
        Pivlist: '',
        Clist: '',
        shortName: '',
        status: ''
      }
      this.getList()
    },
    addUser () {
      this.visible = true
      this.titleInfo.text = ''
      this.titleInfo.pageTitle = '新增用户'
    },
    async addSubmit (formBase) {
      this.formBase = formBase
      this.getList()
      this.visible = false
    },
    async edit (row) {
      const { data } = await detail(row)
      this.formBase = { ...data, isFamous: true }
      this.titleInfo.text = ''
      this.titleInfo.pageTitle = '编辑用户'
      this.visible = true
    },
    next () {
      if (this.page.page > this.page.pages) return
      this.page.page++
      this.getList()
    },
    prev () {
      if (this.page.page === 1) return
      this.page.page--
      this.getList()
    },
    async views (row) {
    },
    Newstate (state) {
      let Cstate = '启用'
      if (state === 1) Cstate = '启用'
      if (state === 0) Cstate = '禁用'
      return Cstate
    },
    isState (row) {
      const that = this
      this.tableData = row
      if (row.state === 0) {
        this.$confirm('已成功启用,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          that.tableData.state = 1
          // console.log(that.tableData)
          // that.getList()
          this.$message({
            type: 'success',
            message: '已成功启用!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
      }
      if (row.state === 1) {
        this.$confirm('已成功禁用,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          that.tableData.state = 0
          // console.log(111)
          // that.getList()
          this.$message({
            type: 'success',
            message: '已成功禁用!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
      }
    },
    // stateSubmit (state) {
    //   if (state === 1) {
    //     this.row.state = 0
    //     this.$message.success('已成功禁用')
    //   } else {
    //     this.row.state = 1
    //     this.$message.success('已成功启用')
    //   }
    //   this.Svisible = false
    // },

    async del (row) {
      this.$confirm('此操作将永久删除用户 , 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(row)
        this.getList()
        this.$message({
          type: 'success',
          message: '成功删除了该用户!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    wait () {
      this.$message('已取消操作！')
      this.Svisible = false
    },
    openSelect () {
      this.formInline.Pivlist = provinces(data)
    },
    changeSelect (Pivlist) {
      this.formInline.Pivlist = Pivlist
      console.log(this.formInline.Pivlist)
    },
    openSelect2 () {
      this.formInline.Clist = citys(this.formInline.Pivlist)
    },
    changeSelect2 () {
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  margin: 5px 10px 0 10px;
  padding: 20px;
  background-color: #fff;
  .count {
    margin-bottom: 15px;
    background-color: #f4f4f5;
    color: #909399;
    width: 100%;
    padding: 8px 16px;
    border-radius: 4px;
    align-items: center;
  }
  .btn {
    // display: flex;
    // justify-content: space-between;
    position: relative;
    .add {
      position: absolute;
      right: -200px;
      // margin-left: 350px;
    }
  }
::v-deep .el-dialog__header {
  // background: #fff;
  .el-dialog__title {
  color: #333;
  }
  .el-dialog__close {
  color: #333;
  }
  .el-icon-warning-outline {
    font-size: 20px !important;
  }
}
.el-dialog__body {
  color: #333;
  font-size: 14px;
}
}
</style>
